import { login, showModal, showToast } from '../../utils/asyncWx'
import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    needBindPhone: ''
  },
  phone: '',
  //需要传递的参数
  QueryParams: {
    user: {
      "userNickName": "",
      "userGender": 1,
      "userHeadPortrait": ""
    },
    code: ''
  }
  ,
  /**
   * 微信手机号快捷登录
   * 
   * @param {*} ev 
   */
  async handleGetUserInfo(e) {
    try {
      const { userInfo } = e.detail
      //wx.setStorageSync("userInfo", userInfo);
      const { nickName, gender, avatarUrl } = e.detail.userInfo
      this.QueryParams.user.userNickName = nickName
      this.QueryParams.user.userGender = gender
      this.QueryParams.user.userHeadPortrait = avatarUrl
      const { code } = await login()
      this.QueryParams.code = code
      //let user = { userNickName, userGender, userHeadPortrait }
      //let user = { userNickName: userInfo.nickName, userGender: parseInt(userInfo.gender), userHeadPortrait: userInfo.avatarUrl }
      //let loginParams = { nickName, gender, avatarUrl, code }
      //loginParams = JSON.stringify(loginParams)
      //user = JSON.stringify(loginParams)
      const res = await request({ url: '/login/miniapp', method: 'POST', data: this.QueryParams })
      const { token, needBindPhone } = res.data
      this.setData({
        needBindPhone
      })
      //console.log(needBindPhone)
      const header = { token }
      const phone = this.phone
      if (needBindPhone) {
        await showToast({ title: '请填写手机号' })
        const res1 = await request({ url: '/login/bind/phone', data: phone, method: 'PUT', header })
      } else {
        await request({ url: '/login/bind/phone', method: 'PUT', header })
      }

      // if (res.needBindPhone && phone) {
      //   const res = await request({ url: '/login/bind/phone', data: { phone }, method: 'PUT', header })
      //   console.log(res)
      // } else {
      //   await showToast({ title: '请填写手机号' })
      // }

      // const res1 = showModal({content:'是否要绑定手机号'})
      //const header = { token }
      //console.log(res)
      wx.setStorageSync("token", token);
      // const res = await request({ url: '/user/address/personalList', header })
      // console.log(res)
      wx.navigateBack({
        delta: 1
      });
    } catch (error) {
      console.log(error)
      await showToast({ title: '不登录的话在使用中会有不便!' })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
      }, 500)

    }

  },
  getPhoneNumber(e) {
    const { value } = e.detail
    this.phone = value
  },
  //关闭弹窗
  onClose() {
    this.setData({
      show: false
    })
  },
  /**
   * 登录/注册
   * @param {*} ev 
   */
  getUserInfo(ev) {
    console.log(ev)
    const { userInfo } = ev.detail
    // 同意授权
    if (userInfo) {

    } else {

    }

    wx.login({
      success(res) {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})